﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Chisel.Core.Connections
{
	/// <summary>
	/// Decorates an endpoint.
	/// </summary>
	public abstract class Decorator : Canvas
	{
		/// <summary>
		/// The endpoint that this decorator is decorating.
		/// </summary>
		public EndPoint EndPoint { get; set; }


		/// <summary>
		/// Lays out the decoration based on the current endpoint.
		/// </summary>
		public abstract void Layout();

	}


	/// <summary>
	/// Decorator for Carlis-style "chicken leg" connections.
	/// </summary>
	public class ChickenLegDecorator : Decorator
	{

		public ChickenLegDecorator()
			: base()
		{
			_line = new Polyline() {
				Stroke = new SolidColorBrush(Colors.Black)
			};
			_line.Points.Add(new Point(0, 0));
			_line.Points.Add(new Point(0, 0));
			_line.Points.Add(new Point(0, 0));
			Children.Add(_line);
		}

		private Polyline _line;

		static ChickenLegDecorator()
		{
			Offset = 10;
		}

		/// <summary>
		/// The offset between fingers.
		/// </summary>
		public static double Offset { get; set; }

		public override void Layout()
		{
			if (EndPoint == null || EndPoint.Direction == null)
				return;
			var direction = (Point)EndPoint.Direction;
			if (direction.X == 0 && direction.Y > 0)
			{
				_line.Points[0] = new Point(-Offset, 0);
				_line.Points[1] = new Point(0, -Offset);
				_line.Points[2] = new Point(Offset, 0);
			}
			else if (direction.X == 0 && direction.Y < 0)
			{
				_line.Points[0] = new Point(-Offset, 0);
				_line.Points[1] = new Point(0, Offset);
				_line.Points[2] = new Point(Offset, 0);
			}
			else if (direction.X > 0 && direction.Y == 0)
			{
				_line.Points[0] = new Point(0, -Offset);
				_line.Points[1] = new Point(Offset, 0);
				_line.Points[2] = new Point(0, Offset);
			}
			if (direction.X < 0 && direction.Y == 0)
			{
				_line.Points[0] = new Point(0, -Offset);
				_line.Points[1] = new Point(-Offset, 0);
				_line.Points[2] = new Point(0, Offset);
			}
		}
	}


	/// <summary>
	/// Chicken leg decorator with the bar for identity.
	/// </summary>
	public class IdentityChickenLegDecorator : ChickenLegDecorator
	{

		public IdentityChickenLegDecorator()
			: base()
		{
			_line = new Polyline() {
				Stroke = new SolidColorBrush(Colors.Black)
			};
			_line.Points.Add(new Point(0, 0));
			_line.Points.Add(new Point(0, 0));
			Children.Add(_line);
		}

		private Polyline _line;

		public override void Layout()
		{
			base.Layout();
			if (EndPoint == null || EndPoint.Direction == null)
				return;
			var direction = (Point)EndPoint.Direction;
			if (direction.X == 0 && direction.Y > 0)
			{
				_line.Points[0] = new Point(-Offset, -2*Offset);
				_line.Points[1] = new Point(Offset, -2*Offset);
			}
			else if (direction.X == 0 && direction.Y < 0)
			{
				_line.Points[0] = new Point(-Offset, 2 * Offset);
				_line.Points[1] = new Point(Offset, 2 * Offset);
			}
			else if (direction.X > 0 && direction.Y == 0)
			{
				_line.Points[0] = new Point(2 * Offset, -Offset);
				_line.Points[1] = new Point(2 * Offset, Offset);
			}
			if (direction.X < 0 && direction.Y == 0)
			{
				_line.Points[0] = new Point(-2 * Offset, -Offset);
				_line.Points[1] = new Point(-2 * Offset, Offset);
			}
		}
	}

}
